RedEmpresa = function(){
	var index = 0;
	return {
		safeForm : function(){
			if(index == 0)
				Asg.Acciones.validarEmpresa();
			if(index == 1)
				Asg.Acciones.guardarEmpresa();
			
		},
		next: function(){
			var form = Ext.getCmp("asistenteEmpresas"); 
			Ext.getCmp("btnBack").show();
			if(index < form.items.length-1){
				index++;
				var cardlayout = form.getLayout();
				cardlayout.setActiveItem(index);
				if(index == form.items.length-1){
					Ext.getCmp("btnValidate").hide();
					Ext.getCmp("btnFinish").show();
				}
			}
		},
		back: function(){
			var form = Ext.getCmp("asistenteEmpresas"); 
			if(index > 0){
				index --;
				var cardlayout = form.getLayout();
				cardlayout.setActiveItem(index);
			}
			
			if(index == 0){
				Ext.getCmp("btnBack").hide();
				Ext.getCmp("btnValidate").show();
				Ext.getCmp("btnFinish").hide();
			}else{
				Ext.getCmp("btnValidate").show();
				Ext.getCmp("btnFinish").hide();
			}
		},
		registroEmpresasWindow: function(){
			index = 0;
			new Ext.Window({
				id: "registroEmpresasWindow",
				layout: "fit", 
				title: Asg.constants.APPLICATION_TITLE + " .::. ASISTENTE DE EMPRESAS",
				modal: true,
				width: 700,
				maximizable: true,
				resizable	: true,
	        	bodyStyle: "background: #DFE8F6",
	        	items:  new Ext.FormPanel({
	    			layout: "card",
	    			border: false,
	    			id: "asistenteEmpresas",
	    			frame: true,
	    			activeItem: RedEmpresa.index,
	    			items: [
	    			    RedEmpresasForms.seccionControlEmpresas(),
	    			    RedEmpresasForms.seccionDatosEmpresa(),
	    			    RedEmpresasForms.seccionMediosBusquedaEmpresa(),
	    			    RedEmpresasForms.seccionSucursalesEmpresa(),
	    			    RedEmpresasForms.seccionContactosEmpresa()
	     		    ]
	    		}),
	    		fbar: [
	    		       new Ext.Button({
	    		    	   text : "Validar Informaci\u00F3n",
	    		    	   id: "btnValidate",
	    		    	   name: "btnValidate",
	    		    	   handler : function(){RedEmpresa.safeForm();},
	    		    	   scope : this
    		    	   }),
    		    	   new Ext.Button({
    		    		   text: "Atras",
    		    		   id: "btnBack",
    		    		   name: "brnBack",
    		    		   hidden: true,
    		    		   handler: function(){RedEmpresa.back();},
    		    		   scope: this
    		    	   }),
    		    	   new Ext.Button({
    		    		   text: "Finalizar",
    		    		   id: "btnFinish",
    		    		   name: "btnFinish",
    		    		   hidden: true,
    		    		   handler: function(){},
    		    		   scope: this
    		    	   })
		        ]
			}).show();
		},
		initConfig: function(config){
			config = config || {};
			Ext.define("redEmpresasModel",{
				extend: "Ext.data.Model",
				fields:[
		        	"redempresafolio","redempresanit","redempresaestado",
		        	"redempresafecharegistro",
		        	"redempresarazonsocial","redempresanombrecomercial",
		        	"redempresatelefono1"
		        ]
			});
			var redEmpresaStore = Ext.create("Ext.data.Store", {
				autoLoad: {params:{start: 0, limit: RedNacionalEmpleo.getPagingSize() || 30}},
				pageSize: RedNacionalEmpleo.getPagingSize() || 30,
				model: "redEmpresasModel",
				proxy: {
			        type: "ajax",
			        url: Asg.Utils.formatString("{0}/admin/empresas/buscarEmpresas", Asg.constants.CONTEXT_PATH),
			        reader: {
			        	type: "json",
			        	root: "empresas",
			        	totalProperty: "totalResult"
	        		}
			    },
			    listeners: {
			    	beforeload: function(store, operation, options){
			    		if(!operation) operation = {};
			    		if(!operation.params) operation.params = {};
			    		if(Asg.Utils.getValue("opcion"))
			    			operation.params["opcion"] = Asg.Utils.getValue("opcion");
			    		if(Asg.Utils.getValue("estado"))
			    			operation.params["estado"] = Asg.Utils.getValue("estado");
			    		if(Asg.Utils.getValue("nombreComercial"))
			    			operation.params["nombreComercial"] = Asg.Utils.getValue("nombreComercial");
			    	}
			    },
			    groupers: [{property: "redempresafecharegistro"}]
			});
			return {
				items: {
					xtype: 'grid',
					id: 'gridEmpresas',
					flex: 1,
					margins: "5 5 5 5",
					store: redEmpresaStore,
					title: 'CONSULTA DE EMPRESAS',
					columns: [
				          {header: "Nombre Comercial", dataIndex: "redempresanombrecomercial", width: 200},
				          {header: "Razon Social", dataIndex: "redempresarazonsocial", width: 200},
				          {header: "Ciudad", dataIndex: "ctgmunicipionombre", width: 175},
				          {
					        	xtype:"actioncolumn",
					        	width: 150,
			        			items: [
		        			        {
		        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/edit.png",
		        			        	tooltip: "EDITAR EMPRESA",
		        			        	iconCls: "action_icons"
		        			        }, {
		        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/page_pdf.png",
		        			        	tooltip: "VER FICHA",
		        			        	iconCls: "action_icons"
		        			        },{
		        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/verification.png",
		        			        	tooltip: "ACTIVAR / INACTIVAR",
		        			        	iconCls: "action_icons"
		        			        },{
		        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/folder_add.png",
		        			        	tooltip: "AGREGAR OFERTA",
		        			        	iconCls: "action_icons"
		        			        },{
		        			        	icon: Asg.constants.CONTEXT_PATH + "/resources/images/cancel.png",
		        			        	tooltip: "ELIMINAR OFERTA",
		        			        	iconCls: "action_icons"
		        			        }
		        			        
	        			        ]
				          }
					],
					plugins: [{
						 ptype: 'rowexpander',
						 rowBodyTpl : [
			             ]
					}],
					features: [
			           {
			        	   ftype: "grouping",
			        	   enableNoGroups: false,
			        	   groupHeaderTpl: ' -> {rows.length} Empresas{[values.rows.length > 1 ? "s" : ""]}'
			           }
		            ],
					tbar: [
	        	       "->",
	        	       {
	        	    	   text: "Agregar Empresa", 
	        	    	   iconCls: Asg.constants.ADD_ICON,
	        	    	   handler: function(){
	        	    		   RedEmpresa.registroEmpresasWindow();
	        	    	   }
	        	       },
	        	       {
	           	        	text: "Buscar",
	           	        	iconCls: Asg.constants.BUSCAR_ICON,
	           	        	handler: function(){
	           	        		Asg.Acciones.buscarEmpresas();
	           	        	}
	           	        }
        	       ],
        	       dockedItems: [{
		                xtype: "pagingtoolbar",
		                store: redEmpresaStore,
		                dock: "bottom",
		                displayInfo: true
		            },{
		            	xtype: "toolbar",
		            	dock: "top",
		            	items: [
	            	       "<b>EMPRESAS</b>",
	            	       {
	            	    	    xtype: "combobox",
		        	        	name: "opcion",
		        	        	id: "opcion",
		        	        	width: 205,
		        	        	labelWidth: 175,
		        	        	margins: "0 0 0 6", 
		        	        	value: 1,
								afterLabelTextTpl:  Asg.constants.REQUIRED,
								allowBlank:false,
								store: new Ext.data.SimpleStore({
									data: [[1,"Mis Empresas"],[2,"Empresas de di Oficina"],[3,"Todas"]] , 
									fields: ["id","value"]
								}),
								valueField: "id",
								displayField: "value"
	            	       },
	            	       "<b>ESTADO</b>",
	            	       {
	            	    	   	xtype: "combobox",
		        	        	name: "estado",
		        	        	id: "estado",
		        	        	width: 205,
		        	        	margins: "0 0 0 6", 
		        	        	value: "1",
								afterLabelTextTpl:  Asg.constants.REQUIRED,
								allowBlank:false,
								store: new Ext.data.SimpleStore({
									data: [["1","ACTIVAS"],["0","INACTIVAS"]] , 
									fields: ["id","value"]
								}),
								valueField: "id",
								displayField: "value"
	            	       },
	            	       "<b>NOMBRE COMERCIAL</b>",
	            	       {
	            	    	   xtype: "textfield",
	            	    	   name: "nombreComercial",
	            	    	   id: "nombreComercial",
	            	    	   margins: "0 0 0 6",
	            	    	   width: 205
	            	       }
		            	]
		            }]
				}
			};
		}
	};
}();